import {Injectable} from '@nestjs/common';
import {InjectRepository} from '@nestjs/typeorm';
import {Repository} from 'typeorm';
import {User} from './entities/user.entity';

export type UserAuth = any;

@Injectable()
export class UsersService {
    private readonly usersAuth: UserAuth[]  // 应该在这里
    constructor(
        // private readonly usersAuth: UserAuth[],  // 不应该在这里
        @InjectRepository(User)
        private usersRepository: Repository<User>
    ) {
        this.usersAuth = [
            {
                userId: 1,
                username: 'john',
                password: 'changeme',
                isActive: true,
            },
            {
                userId: 2,
                username: 'chris',
                password: 'secret',
                isActive: true,
            },
            {
                userId: 3,
                username: 'maria',
                password: 'guess',
                isActive: true,
            },
        ];
    }

    findAll(): Promise<User[]> {
        return this.usersRepository.find();
    }

    findOne(id: number): Promise<User> {
        // @ts-ignore
        return this.usersRepository.findOne(id);
    }

    async remove(id: number): Promise<void> {
        await this.usersRepository.delete(id);
    }

    async findOneAuth(username: string): Promise<User | undefined> {
        return this.usersAuth.find(user => user.username === username);
    }
}
